package com.py.credit.controller;

import com.py.credit.common.Page;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.CswhObj;
import com.py.credit.entity.DictEntity;
import com.py.credit.service.CswhService;
import com.py.credit.service.LhjcRemoteBaseService;
import com.py.credit.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 措施controller
 */
@Controller
@RequestMapping(value = "/cswh")
public class CswhController {

    @Autowired
    private CswhService cswhService;

    @Autowired
    private LhjcRemoteBaseService lhjcRemoteBaseService;

    /**
     * 部门列表(一级部门)
     *
     * @param code 部门编码
     * @param name 部门名称
     * @param page 分页条件
     * @return
     */
    
	@ResponseBody
    @RequestMapping(value = "/deptList.json")
    public RestServiceResult getDeptList(@RequestParam(defaultValue = "", required = true, value = "code") String code,
                                         @RequestParam(defaultValue = "", required = true, value = "name") String name,
                                         Page page) {
    	
    	List<Map<String, String>> lists = cswhService.selectDeptList(code, name, page);
    	
        return new RestServiceResult<>(page.getTotalCount() , lists);
    }

    /**
     * 加载部门树
     *
     * @param name 部门名称
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deptTreeList.json")
    public RestServiceResult getDeptTreeList(@RequestParam(defaultValue = "", required = true, value = "name") String name) {
        return new RestServiceResult(cswhService.selectDeptTreeList(name));
    }

    /**
     * 加载领域树
     *
     * @param name 领域名称
     * @param type 奖惩类别：C:惩戒，J:激励
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/lyTreeList.json")
    public RestServiceResult getLyTreeList(@RequestParam(defaultValue = "", required = true, value = "name") String name,
                                           @RequestParam(defaultValue = "", required = true, value = "type") String type) {
        return new RestServiceResult(cswhService.selectLyTreeList(name, type));
    }

    /**
     * 新增措施
     *
     * @param jclb   奖惩类别：C:惩戒，J:激励
     * @param cs     措施手段
     * @param remark 惩戒措施
     * @param yj     法律及政策依据
     * @param jczt   奖惩主体：C：法人 Z：自然人 A:所有
     * @param csbm   措施编码
     * @param csType 措施列别：G：公共指标 P：普通指标
     * @param sort   排序值
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCs.json")
    public RestServiceResult add(@RequestParam(defaultValue = "", required = true, value = "jclb") String jclb,
                                 @RequestParam(defaultValue = "", required = true, value = "cs") String cs,
                                 @RequestParam(defaultValue = "", required = true, value = "remark") String remark,
                                 @RequestParam(defaultValue = "", required = true, value = "yj") String yj,
                                 @RequestParam(defaultValue = "", required = true, value = "jczt") String jczt,
                                 @RequestParam(defaultValue = "", required = true, value = "csbm") String csbm,
                                 @RequestParam(defaultValue = "", required = true, value = "csType") String csType,
                                 @RequestParam(defaultValue = "", required = true, value = "sort") String sort) {
        if (cswhService.selectSameCode(csbm, remark) > 0) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "存在相同的措施编码或措施名称，请核对");
        } else {
            String id = UuidUtil.getInstance().getUUid();
            cswhService.add(id, jclb, cs, remark, yj, jczt, csbm, sort, csType, lhjcRemoteBaseService.getDictByDtlKey("CSSD", cs).getData().getDtlValue());
            if (csType.equals("G")) {
                cswhService.addCsDept(jclb, "-1", id);
            }
            return new RestServiceResult(RestServiceResult.CODE_SUCCESS, "");
        }
    }

    /**
     * 修改措施
     *
     * @param id     措施id
     * @param cs     措施手段
     * @param remark 惩戒措施
     * @param yj     法律及政策依据
     * @param jczt   奖惩主体：C：法人 Z：自然人 A:所有
     * @param csbm   措施编码
     * @param sort   排序值
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modifyCs.json")
    public RestServiceResult modifyCs(@RequestParam(defaultValue = "", required = true, value = "id") String id,
                                      @RequestParam(defaultValue = "", required = true, value = "cs") String cs,
                                      @RequestParam(defaultValue = "", required = true, value = "remark") String remark,
                                      @RequestParam(defaultValue = "", required = true, value = "yj") String yj,
                                      @RequestParam(defaultValue = "", required = true, value = "jczt") String jczt,
                                      @RequestParam(defaultValue = "", required = true, value = "csbm") String csbm,
                                      @RequestParam(defaultValue = "", required = true, value = "sort") String sort) {
        if (cswhService.selectOtherSameCode(id, csbm, remark) > 0) {
            return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "存在相同的措施编码或措施名称，请核对");
        } else {
            cswhService.modify(id, cs, remark, yj, jczt, csbm, sort, lhjcRemoteBaseService.getDictByDtlKey("CSSD", cs).getData().getDtlValue());
            return new RestServiceResult(RestServiceResult.CODE_SUCCESS);
        }
    }

    /**
     * 删除措施
     *
     * @param type 奖惩类别：C:惩戒，J:激励
     * @param ids  措施id(多个id逗号拼接)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delCs.json")
    public RestServiceResult delCs(@RequestParam(defaultValue = "", required = true, value = "type") String type,
                                   @RequestParam(defaultValue = "", required = true, value = "ids") String ids) {
        List<String> idss = Arrays.asList(ids.split(","));
        for (String id : idss) {
            int num = cswhService.selectBdGzCs(id);
            if (num > 0) {
                return new RestServiceResult(RestServiceResult.CODE_UNKNOW, "措施绑定了规则，无法删除");
            }
        }
        return cswhService.del(type, ids);
    }

    /**
     * 通过id获取措施对象
     *
     * @param id 措施id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectOneById.json")
    public RestServiceResult selectOne(@RequestParam(defaultValue = "", required = true, value = "id") String id) {
        return new RestServiceResult(cswhService.selectOneById(id));
    }

    /**
     * Excel统计导出
     *
     * @param response
     * @param type     奖惩类别：C:惩戒，J:激励
     */
    @ResponseBody
    @RequestMapping(value = "/exportExcel.xls")
    public void ExcelExport(HttpServletResponse response,
                            @RequestParam(value = "type", defaultValue = "", required = true) String type) {
        cswhService.excelExport(response, type);
    }

    /**
     * 规则添加页面措施列表
     *
     * @param id     措施id
     * @param page   分页条件
     * @param jclb   奖惩类别：C:惩戒，J:激励
     * @param csbm   措施编码
     * @param remark 措施名称
     * @param deptId 部门id
     * @param lyId   领域id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pageList.json")
    public RestServiceResult getListForPage(@RequestParam(value = "id", defaultValue = "", required = true) String id,
                                            @RequestParam(value = "jclb", defaultValue = "", required = true) String jclb,
                                            @RequestParam(value = "csbm", defaultValue = "", required = true) String csbm,
                                            @RequestParam(value = "remark", defaultValue = "", required = true) String remark,
                                            @RequestParam(value = "deptId", defaultValue = "", required = true) String deptId,
                                            @RequestParam(value = "lyId", defaultValue = "", required = true) String lyId, Page page) {
        if ("undefined".equals(lyId)) {
            lyId = "";
        }
        List<CswhObj> cswhObjList = cswhService.getListForPage(id, jclb, csbm, remark, deptId, lyId, page);
        return new RestServiceResult(page.getTotalCount(), cswhObjList);
    }

    /**
     * 获取措施手段类型列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCssdTypes.json")
    public RestServiceResult<List<DictEntity>> getFeedbackTypes() {
        try {
            return lhjcRemoteBaseService.getDictByDictKey("CSSD");
        } catch (Exception e) {
            RestServiceResult<List<DictEntity>> result = new RestServiceResult<>();
            ExceptionLogger.error("CswhController#getCssdTypes", "获取措施手段类型异常！ERROR:" + e.getMessage(), e);
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("获取措施手段类型异常！");
            return result;
        }
    }
}
